﻿using System;
using System.Drawing;
using FluentParser.Expressions.Rules.Decoration;

namespace FluentParser.Expressions
{
    /// <summary>
    /// Base typeless
    /// </summary>
    public interface IBaseExpr
    {
        //
        void ParseNode(GrammarContext context, ParsingNodeResult node);

        /// <summary> The type of the operation applied by this expression. </summary>
        OperationType TypeOperation { get; }
        
        /// <summary> The color used for syntax highlighting </summary>
        Color? Color { get; }
    }

    public abstract class BaseExpr<T> : IBaseExpr
    {
        public ParsingResult<T> Parse(string text)
        {
            return Parse(new GrammarContext(text), 0);
        }

        public abstract ParsingResult<T> Parse(GrammarContext context, int index);
        public abstract void ParseNode(GrammarContext context, ParsingNodeResult node);
        public OperationType TypeOperation { get; protected set;  }

        public Color? Color { get; set; }

        public Expr<TNew> Cast<TNew>()
        {
            return new CastToExpressionRule<T, TNew>(this);
        }

        //public Expr<TR> Convert<TR>(TR value)
        //{
        //    return new ConvertExpr<T, TR>(this, x => value);
        //}

        public Expr<TR> Convert<TR>(Func<T, TR> func)
        {
            return new ConvertExpressionRule<T, TR>(this, func);
        }
    }
}